﻿using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Tilver.Coder.Core;

namespace Tilver.Coder.Utility
{
    public class CommonUtil
    {
        /// <summary>
        /// 保存的项目文件扩展名
        /// </summary>
        public const string ProjectFileExtension = ".tvc";

        /// <summary>
        /// 保存项目
        /// </summary>
        /// <param name="project">项目</param>
        /// <param name="fileName">保存的文件路径</param>
        public static void SaveProject(CoderProject project, string fileName)
        {
            var xmlSerializer = new XmlSerializer(typeof(CoderProject));
            using (var writer = new XmlTextWriter(fileName, Encoding.UTF8))
            {
                xmlSerializer.Serialize(writer, project);
            }
        }
        /// <summary>
        /// 加载项目
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static CoderProject LoadProject(string fileName)
        {
            CoderProject project = null;
            var xmlSerializer = new XmlSerializer(typeof (CoderProject));
            using (var reader = new XmlTextReader(fileName))
            {
                project = xmlSerializer.Deserialize(reader) as CoderProject;
            }
            return project;
        }
    }
}
